// 设置基地址常量
const baseURL = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'

import { useMemberStore } from '@/stores/modules/member'
//添加拦截器
const httpInterceptor = {
  // 拦截前触发
  invoke(options: UniApp.RequestOptions) {
    // 1、非http开头需要拼接
    if (!options.url.startsWith('http')) {
      options.url = baseURL + options.url
    }
    // 2、请求超时,默认60s，我们设10s
    options.timeout = 1000

    // 3、添加小程序端请求标识
    options.header = {
      ...options.header,
      'source-client': 'miniapp',
    }
    //4、添加token请求头
    const memberStore = useMemberStore()
    const token = memberStore.profile?.token
    if (token) {
      options.header.Authorization = token
    }
    console.log(options)
  },
}
uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)
